package com.gk.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.study.entity.Record;
import com.gk.study.mapper.RecordMapper;
import com.gk.study.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {
    @Autowired
    RecordMapper mapper;

    @Override
    public Record getRecord(Long thingId, String ip) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("thing_id", thingId);
        queryWrapper.eq("ip", ip);
        return mapper.selectOne(queryWrapper);
    }

    @Override
    public void createRecord(Record record) {
        mapper.insert(record);
    }

    @Override
    public void updateRecord(Record record) {
        mapper.updateById(record);
    }

    @Override
    public List<Record> getRecordListByIp(String ip) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ip", ip);
        return mapper.selectList(queryWrapper);
    }

    @Override
    public List<String> getRecordIpList() {
        // 查询所有Record，提取ip字段并去重
        List<Record> allRecords = mapper.selectList(null);
        return allRecords.stream().map(Record::getIp).distinct().collect(Collectors.toList());
    }
}
